package com.example.demo.config;

import com.example.demo.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ResponseBody//要加@ResponseBody这个注解
@ControllerAdvice
public class ErrorHandler {
    @ExceptionHandler
    public Result handler(Exception e) {
        log.info("捕获异常：e:", e);
        return Result.fail("内部发生错误:" + e.getMessage());
    }
    @ExceptionHandler
    public Result handler(NullPointerException e) {
        log.info("捕获异常：e:", e);
        return Result.fail("内部发生错误:" + e.getMessage());
    }
    @ExceptionHandler
    public Result handler(RuntimeException e) {
        log.info("捕获异常：e:", e);
        return Result.fail("内部发生错误:" + e.getMessage());
    }@ExceptionHandler
    public Result handler(ArithmeticException e) {
        log.info("捕获异常：e:", e);
        return Result.fail("内部发生错误:" + e.getMessage());
    }

}
